package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartInfoService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2025-05-07 15:50
 */

@RestController
public class CartController {


    @Autowired
    private CartInfoService cartService;

    /**
     * 当前登录用户加购
     *
     * @param skuId
     * @param num
     * @return
     */
    @Operation(summary = "当前登录用户加购")
    @GetMapping("/addToCart/{skuId}/{num}")
    public AjaxResult addToCart(
            @PathVariable("skuId") Long skuId,
            @PathVariable("num") Integer num) {
        cartService.addToCart(skuId, num);
        return AjaxResult.success();
    }

    /**
     * 查询购物车商品列表
     * @return
     */
    @Operation(summary = "查询购物车商品列表")
    @GetMapping("/cartList")
    public AjaxResult cartList() {
        List<CartInfo> cartList = cartService.cartList();
        return AjaxResult.success(cartList);
    }

    /**
     * 选中或取消选中状态
     * @param skuId
     * @param isChecked
     * @return
     */
    @Operation(summary = "选中或取消选中状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(
            @PathVariable("skuId") Long skuId,
            @PathVariable("isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return AjaxResult.success();
    }


    /**
     * 全部选中或取消选中状态
     * @param isChecked
     * @return
     */
    @Operation(summary = "全部选中或取消选中状态")
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable("isChecked") Integer isChecked) {
        cartService.allCheckCart(isChecked);
        return AjaxResult.success();
    }

    /**
     * 删除购物车商品
     * @param skuId
     * @return
     */
    @Operation(summary = "删除购物车商品")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult  deleteCart(@PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return AjaxResult.success();
    }

    /**
     * 清空购物车
     * @return
     */
    @Operation(summary = "清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart() {
        cartService.clearCart();
        return AjaxResult.success();
    }


    @Operation(summary = "查询用户购物车列表中选中商品列表")
    @InnerAuth
    @GetMapping("/getCartCheckedList")
    public R<List<CartInfo>> getCartCheckedList() {
        return R.ok(cartService.getCartCheckedList());
    }


    @InnerAuth
    @Operation(summary="更新用户购物车列表价格")
    @GetMapping("/updateCartPrice")
    public R<Boolean> updateCartPrice(){
        return R.ok(cartService.updateCartPrice());
    }

    @InnerAuth
    @Operation(summary="删除用户购物车列表中选中商品列表")
    @GetMapping("/deleteCartCheckedList")
    public R<Boolean> deleteCartCheckedList(){
        return R.ok(cartService.deleteCartCheckedList());
    }
}
